﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace LinqProviders.Utility
{
    public static class HttpReqeust
    {

        /// <summary>
        /// Executes the web request.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <returns></returns>
        public static string ExecuteWebRequest(Uri uri)
        {
            string responseData = null;

            //TODO: Exception Handling

            System.Diagnostics.Debug.Write("Web Requset Uri : " + uri.ToString());

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

            if (webResponse.StatusCode == HttpStatusCode.OK)
            {
                Encoding encoding;
                string charSet = string.Empty;

                charSet = webResponse.CharacterSet;

                if (String.IsNullOrEmpty(charSet))
                    encoding = Encoding.Default;
                else
                    encoding = Encoding.GetEncoding(charSet);

                using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), encoding))
                {
                    responseData = sr.ReadToEnd();
                }
            }
            else
            {
                //TODO: Do Somthing When Response Code not OK, Write Logg 
                ;
            }
            return responseData;
        }

    }
}
